package co.yixiang.huiMin.controller;


import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.huiMin.dao.MachinistQueryCriteria;
import co.yixiang.huiMin.entity.Classify;
import co.yixiang.huiMin.entity.Machinist;
import co.yixiang.huiMin.entity.UserApply;
import co.yixiang.huiMin.repository.UserApplyRepository;
import co.yixiang.huiMin.service.UserApplyService;
import co.yixiang.modules.shop.service.dto.YxUserDTO;
import co.yixiang.modules.system.domain.DictDetail;
import co.yixiang.modules.system.repository.DictDetailRepository;
import co.yixiang.modules.system.service.dto.DictDetailQueryCriteria;
import co.yixiang.modules.system.service.mapper.DictDetailMapper;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 机修学堂 前端控制器
 * </p>
 *
 * @author Author
 * @since 2020-07-01
 */
@RestController
@Api(tags = "机修学堂")
@RequestMapping("/userApply")
public class UserApplyController {


    @Resource
    private DictDetailRepository dictDetailRepository;
    @Resource
    private UserApplyService userApplyService;

    @Resource
    private UserApplyRepository userApplyRepository;
    /**
     * 查询机修学堂
     * @return
     */
    @AnonymousAccess
    @ApiOperation("查询机修学堂")
    @GetMapping("/selectUserApplys")
    public ResponseEntity<Object> selectUserApplys(Pageable pageable){

        MachinistQueryCriteria machinistQueryCriteria = new MachinistQueryCriteria();
        Page<UserApply> machinists = userApplyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, machinistQueryCriteria, criteriaBuilder), pageable);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("message", PageUtil.toPage(machinists));
        stringObjectHashMap.put("code",200);
        return new ResponseEntity<>(stringObjectHashMap, HttpStatus.OK);

    }

    /**
     *处理机修学堂
     * @return
     */
    @AnonymousAccess
    @ApiOperation("处理机修学堂")
    @PostMapping("/updateUserApplyS")
    public ResponseEntity<Object> updateUserApplyS(UserApply userApply){

        HashMap<String, Object> stringObjectHashMap = new HashMap<>();


        int count=userApplyService.updateUserApplyS(userApply);
        if(count>0){
            stringObjectHashMap.put("code",200);
            return new ResponseEntity<>(stringObjectHashMap, HttpStatus.OK);
        }
        stringObjectHashMap.put("code",300);
        return new ResponseEntity<>(stringObjectHashMap, HttpStatus.OK);
    }

    /**
     *处理机修学堂
     * @return
     */
    @AnonymousAccess
    @ApiOperation("查询机修学堂信息")
    @GetMapping("/getUserApplyInfo")
    public ResponseEntity<Object> getUserApplyInfo(){
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();


        DictDetailQueryCriteria dictDetailQueryCriteria = new DictDetailQueryCriteria();
        dictDetailQueryCriteria.setLabel("apply");
        DictDetail dictDetail = new DictDetail();
        dictDetail.setLabel("apply");
        Optional<DictDetail> one = dictDetailRepository.findOne((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, dictDetailQueryCriteria, criteriaBuilder));
        dictDetailQueryCriteria.setLabel("applyInfo");
        Optional<DictDetail> two = dictDetailRepository.findOne((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, dictDetailQueryCriteria, criteriaBuilder));
        dictDetailQueryCriteria.setLabel("applyAward");
        Optional<DictDetail> three = dictDetailRepository.findOne((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, dictDetailQueryCriteria, criteriaBuilder));
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("pic", one.get().getValue());
        resultMap.put("notice", two.get().getValue());
        resultMap.put("award", three.get().getValue());
        stringObjectHashMap.put("code",200);
        stringObjectHashMap.put("message",resultMap);
        return new ResponseEntity<>(stringObjectHashMap, HttpStatus.OK);
    }

    /**
     *处理机修学堂
     * @return
     */
    @AnonymousAccess
    @ApiOperation("修改机修学堂信息")
    @PostMapping("/updateUserApplyInfo")
    public ResponseEntity<Object> updateUserApplyInfo(BigDecimal pic,String notice,BigDecimal award){


        HashMap<String, Object> stringObjectHashMap = new HashMap<>();


        DictDetailQueryCriteria dictDetailQueryCriteria = new DictDetailQueryCriteria();
        dictDetailQueryCriteria.setLabel("apply");
        Optional<DictDetail> one = dictDetailRepository.findOne((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, dictDetailQueryCriteria, criteriaBuilder));
        dictDetailQueryCriteria.setLabel("applyInfo");
        Optional<DictDetail> two = dictDetailRepository.findOne((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, dictDetailQueryCriteria, criteriaBuilder));
        dictDetailQueryCriteria.setLabel("applyAward");
        Optional<DictDetail> three = dictDetailRepository.findOne((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, dictDetailQueryCriteria, criteriaBuilder));
        DictDetail apply = one.get();
        DictDetail applyInfo = two.get();
        DictDetail applyAward = three.get();
        apply.setValue(pic.toString());
        dictDetailRepository.save(apply);
        applyInfo.setValue(notice);
        dictDetailRepository.save(applyInfo);
        applyAward.setValue(award.toString());
        dictDetailRepository.save(applyAward);
        stringObjectHashMap.put("code",200);
        return new ResponseEntity<>(stringObjectHashMap, HttpStatus.OK);
    }



    /**
     *查询返佣
     * @return
     */
    @AnonymousAccess
    @ApiOperation("查询返佣")
    @GetMapping("/getUserApplyInfos")
    public ResponseEntity<Object> getUserApplyInfos(){
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        DictDetailQueryCriteria dictDetailQueryCriteria = new DictDetailQueryCriteria();
        dictDetailQueryCriteria.setLabel("rebate");
        DictDetail dictDetail = new DictDetail();
        dictDetail.setLabel("rebate");
        Optional<DictDetail> one = dictDetailRepository.findOne((root, criteriaQuery, criteriaBuilder)
                -> QueryHelp.getPredicate(root, dictDetailQueryCriteria, criteriaBuilder));
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("pic", one.get().getValue());
        stringObjectHashMap.put("code",200);
        stringObjectHashMap.put("message",resultMap);
        return new ResponseEntity<>(stringObjectHashMap, HttpStatus.OK);
    }


    /**
     *修改返佣
     * @return
     */
    @AnonymousAccess
    @ApiOperation("修改返佣")
    @PostMapping("/updateUserApplyInfos")
    public ResponseEntity<Object> updateUserApplyInfos(BigDecimal pic){


        HashMap<String, Object> stringObjectHashMap = new HashMap<>();

        DictDetailQueryCriteria dictDetailQueryCriteria = new DictDetailQueryCriteria();
        dictDetailQueryCriteria.setLabel("rebate");
        Optional<DictDetail> one = dictDetailRepository.findOne((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, dictDetailQueryCriteria, criteriaBuilder));
        DictDetail apply = one.get();
        apply.setValue(pic.toString());
        dictDetailRepository.save(apply);
        stringObjectHashMap.put("code",200);
        return new ResponseEntity<>(stringObjectHashMap, HttpStatus.OK);
    }

}

